import 'dart:io';

import 'package:thermal_printer/thermal_printer.dart';

enum PrinterType { bluetooth, usb, network }

class PrinterManager {
  final bluetoothPrinterConnector = BluetoothPrinterConnector.instance;
  final tcpPrinterConnector = TcpPrinterConnector.instance;
  final usbPrinterConnector = UsbPrinterConnector.instance;

  PrinterManager._();

  static PrinterManager _instance = PrinterManager._();

  static PrinterManager get instance => _instance;

  Stream<PrinterDevice> discovery() {
    return usbPrinterConnector.discovery();
  }

  Future<bool> connect({required BasePrinterInput model}) async {
    try {
      var conn = await usbPrinterConnector.connect(model as UsbPrinterInput);
      return conn;
    } catch (e) {
      throw Exception('model must be type of UsbPrinterInput');
    }
  }

  Future<bool> disconnect({int? delayMs}) async {
    return await usbPrinterConnector.disconnect(delayMs: delayMs);
  }

  Future<bool> print(List<int> bytes) async {
    return await usbPrinterConnector.send(bytes);
  }

  Stream<BTStatus> get stateBluetooth => bluetoothPrinterConnector.currentStatus.cast<BTStatus>();
  Stream<USBStatus> get stateUSB => usbPrinterConnector.currentStatus.cast<USBStatus>();
  Stream<TCPStatus> get stateTCP => tcpPrinterConnector.currentStatus.cast<TCPStatus>();

  BTStatus get currentStatusBT => bluetoothPrinterConnector.status;
  USBStatus get currentStatusUSB => usbPrinterConnector.status;
  TCPStatus get currentStatusTCP => tcpPrinterConnector.status;
}
